//
//  MLXToolUtils.m
//  MLX
//
//  Created by SY on 2018/10/7.
//  Copyright © 2018年 SY. All rights reserved.
//

#import "MLXToolUtils.h"

static MLXToolUtils *_instance;
@interface MLXToolUtils()

@end

@implementation MLXToolUtils
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (_instance == nil) {
            _instance = [super allocWithZone:zone];
        }
    });
    return _instance;
}
+(instancetype)shareTools{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    });
    return _instance;
}
// 为了严谨，也要重写copyWithZone 和 mutableCopyWithZone
-(id)copyWithZone:(NSZone *)zone{
    return _instance;
}
-(id)mutableCopyWithZone:(NSZone *)zone{
    return _instance;
}
+(MLXAppDelegate *)getAppDelegate{
    return (MLXAppDelegate *)[UIApplication sharedApplication].delegate;
}


@end

@implementation NSObject (Model)
/** 获取数据*/
+(id)getSqliteModel{
    NSString* where = [NSString stringWithFormat:@"where %@=%@",bg_sqlKey(@"userName"),bg_sqlValue(@"userName")];
    NSArray* arr = [self bg_find:nil where:where];
    if (arr.count) {
        return arr.firstObject;
    }
    return nil;
}
/** 保存数据*/
+(void)saveSqliteModel:(id)Model{
    if (!Model) return;
    [[MLXDispatchOperation dispatchOperationWithBlock:^{
        [Model setValue:bg_sqlValue(@"userName") forKey:@"userName"];
        [Model bg_saveOrUpdate];
    }] start];
}
@end
